﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.IO;

namespace UnitTestUrl
{
    [TestClass]
    public class UnitTestUrl
    {
        private int statuscode = 0;
        private string statustext = "";
        private string data = "";
        [TestMethod]
        public void InsertNewEntryInDB_WithNonValidUrl_ShouldReturnNull()
        {
            Plugin.Datenbankmanager dbm = new Plugin.Datenbankmanager();
            string url = null;
            string result = dbm.insertNewEntry(url);
            Assert.IsNull(result);
        }

        [TestMethod]
        public void InsertNewEntryInDB_WithValidUrl_ShouldReturnKeyAsString()
        {
            Plugin.Datenbankmanager dbm = new Plugin.Datenbankmanager();
            string url = "www.google.at";
            string result = dbm.insertNewEntry(url);
            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void getUrlByKey_WithNonValidKey_ShouldReturnNull()
        {
            Plugin.Datenbankmanager dbm = new Plugin.Datenbankmanager();
            string key = null;
            string result = dbm.getUrlByKey(key);
            Assert.IsNull(result);
        }

        [TestMethod]
        public void generateRandomString_ShouldReturnString()
        {
            Plugin.Datenbankmanager dbm = new Plugin.Datenbankmanager();
            string result = dbm.randomString(5);
            Assert.IsNotNull(result);
        }

        [TestMethod]
        public void ExecutePlugin_WithNoParameters_ShouldReturnValidResponse()
        {
            TcpListener server = new TcpListener(IPAddress.Any, 55123);

            ParameterizedThreadStart pts = new ParameterizedThreadStart(this.newThreadMethod);
            Thread thread = new Thread(pts);
            thread.Start(server);

            TcpClient client = new TcpClient("127.0.0.1", 55123);
            NetworkStream stream = client.GetStream();
            StreamWriter writer = new StreamWriter(stream);
            writer.WriteLine("GET /url HTTP/1.1");
            writer.WriteLine("Host: localhost");
            writer.WriteLine("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12");
            writer.WriteLine("Connection: close");
            writer.WriteLine();
            writer.WriteLine("");
            writer.Flush();
            client.Close();

            thread.Join(); //warten bis Response vom anderen Thread generiert wurde

            Assert.AreEqual(200, statuscode);
            Assert.AreEqual("OK", statustext, true);
            bool b = data.Contains("Geben Sie Ihre Url in das Textfeld ein und klicken Sie auf generieren, um eine Short-Url zu erstellen.");
            Assert.IsTrue(b);
        }

        private void newThreadMethod(Object parameter)
        {
            TcpListener server = (TcpListener)parameter;
            server.Start();
            Socket so = server.AcceptSocket();
            IPlugin.Request req = new IPlugin.Request(so);
            req.Parse();

            Plugin.Plugin urlPlugin = new Plugin.Plugin();
            IPlugin.Response resp = urlPlugin.ExecutePlugin(req);
            statuscode = resp.StatusCode;
            statustext = resp.StatusText;
            data = resp.Data;

            server.Stop();
        }

    }
}
